home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / hurd / port2fd.c < prev    next >
C/C++ Source or Header  |  1994-05-03  |  3KB  |  85 lines

  1. /* Copyright (C) 1994 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <hurd.h>
  20. #include <hurd/fd.h>
  21. #include <hurd/signal.h>
  22. #include <hurd/term.h>
  23. #include <fcntl.h>
  24.  
  25. /* Store PORT in file descriptor D, doing appropriate ctty magic.
  26.    FLAGS are as for `open'; only O_IGNORE_CTTY is meaningful.
  27.    D should be locked, and will not be unlocked.  */
  28.  
  29. void
  30. _hurd_port2fd (struct hurd_fd *d, io_t port, int flags)
  31. {
  32.   io_t ctty;
  33.   mach_port_t cttyid;
  34.   int is_ctty = !(flags & O_IGNORE_CTTY) && ! __term_getctty (port, &cttyid);
  35.  
  36.   if (is_ctty)
  37.     {
  38.       /* This port is capable of being a controlling tty.
  39.      Is it ours?  */
  40.       struct hurd_port *const id = &_hurd_ports[INIT_PORT_CTTYID];
  41.       __spin_lock (&id->lock);
  42.       if (id->port == MACH_PORT_NULL)
  43.     /* We have no controlling tty, so make this one it.  */
  44.     _hurd_port_locked_set (id, cttyid);
  45.       else
  46.     {
  47.       if (cttyid != id->port)
  48.         /* We have a controlling tty and this is not it.  */
  49.         is_ctty = 0;
  50.       /* Either we don't want CTTYID, or ID->port already is it.
  51.          So we don't need to change ID->port, and we can release
  52.          the reference to CTTYID.  */
  53.       __spin_unlock (&id->lock);
  54.       __mach_port_deallocate (__mach_task_self (), cttyid);
  55.     }
  56.     }
  57.  
  58.   if (is_ctty && ! __term_become_ctty (port, _hurd_pid, _hurd_pgrp,
  59.                        _hurd_msgport, &ctty))
  60.     {
  61.       /* Operations on CTTY return EBACKGROUND when we are not a
  62.      foreground user of the tty.  */
  63.       mach_port_t tmp = port;
  64.       port = ctty;
  65.       ctty = tmp;
  66.     }
  67.   else
  68.     /* XXX if IS_CTTY, then this port is our ctty, but we are
  69.        not doing ctty style i/o because term_become_ctty barfed.
  70.        What to do?  */
  71.     /* No ctty magic happening here.  */
  72.     ctty = MACH_PORT_NULL;
  73.  
  74.   /* Install PORT in the descriptor cell, leaving it locked.  */
  75.   {
  76.     mach_port_t old
  77.       = _hurd_userlink_clear (&d->port.users) ? d->port.port : MACH_PORT_NULL;
  78.     d->port.port = port;
  79.     if (old != MACH_PORT_NULL)
  80.       __mach_port_deallocate (__mach_task_self (), old);
  81.   }
  82.  
  83.   _hurd_port_set (&d->ctty, ctty);
  84. }
  85.